package com.utilities.pipelineScripts;

import java.util.ArrayList;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Element;
import com.utilities.dataStructures.Range;
import com.utilities.parsers.ContigsStatsGenerator;
import com.utilities.visualiser.PrepareXMLFile;


public class Visualizer {

	private static PrepareXMLFile prepareXMLFile = null;
	private static String[] colors = {"maroon","red","blue","green","orange", "purple","fuchsia","navy",
		"lime","olive","yellow","teal","aqua","silver","gray","white","black"};
	
	public static void main(String args[])
	{
		int i = 0;
		String[] names = new String[Integer.parseInt(args[i])];
		int seqLength = Integer.parseInt(args[++i]);
		String summaryfile = args[++i];
		String vxmlfile = args[++i];
		i++;
		ArrayList<ArrayList<Range>> ranges = new ArrayList<ArrayList<Range>>();
		int j = 0;
		for(; i < args.length ; i++)
		{
			ContigsStatsGenerator contigsStatsGenerator = ContigsStatsGenerator.getInstance()
			.setInputScaffoldFile(args[i]).setSeqReadLength(seqLength).setConsiderGaps(true)
			.setOut_summaryFile(summaryfile).setAppendSummary(true).setStrainName(args[i + 1]);
			if (!contigsStatsGenerator.generateStats()) return;
			ArrayList<Range> strain = contigsStatsGenerator.getEachContigInfo();
			ranges.add(strain);
			names[j] = args[++i];
			j++;
		}
		i = (names.length * 2);
		try
		{
			Visualizer.makeVisualisation(ranges, seqLength, names, vxmlfile);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	
	private static void makeVisualisation(ArrayList<ArrayList<Range>> ranges,int seqLength,String[] names, String vXml) 
					throws ParserConfigurationException
	{
		initialise(String.valueOf(seqLength),names);
		makeFeatureSlots(names.length);
		int index = 0;
		for (ArrayList<Range> strain : ranges)
		{
			for(Range range : strain)
			{
				String mouseOver = "Start = " + String.valueOf(range.getLowerLimit())
				+ ", stop = " + String.valueOf(range.getUpperLimit());
				String name = range.getMetadata().split("\t")[0];
				Element feature = prepareXMLFile.createFeature("directStrand" +index, colors[index], "clockwise-arrow", 
						name, null,mouseOver,null,null,null);
				prepareXMLFile.createFeatureRange(feature, String.valueOf(range.getLowerLimit()), 
						String.valueOf(range.getUpperLimit()), null);
			}
			index++;
		}
		prepareXMLFile.writeToFile(vXml);
	}

	private static void initialise(String seqLength, String[] args) throws ParserConfigurationException 
	{
		prepareXMLFile = new PrepareXMLFile().createCGView(seqLength)
		.createLegend("sideLegend", "upper-right", "SanSerif, plain, 10", "0.8");
		
		for (int i = 0; i < args.length; i++)
		{
			String legendLabel = args[i];
			prepareXMLFile = prepareXMLFile.createLegendItem("sideLegend", legendLabel, "SanSerif, plain, 12", true, colors[i]);
		}
		prepareXMLFile = prepareXMLFile.createLegend("lowerleftlegend", "lower-left", "SanSerif, plain, 9", "0.8")
		.createLegendItem("lowerleftlegend", "This map was generated using CGView", null, false, null)
		.createLegend("lowerrightlegend", "lower-right", "SanSerif, plain, 9", "0.8")
		.createLegendItem("lowerrightlegend", "Work done at D-BSSE ETH Zurich", null, false, null);
	}
	
	private static void makeFeatureSlots(int slots) 
	{
		for(int i = 0; i < slots + 1; i++)
		{
			prepareXMLFile = prepareXMLFile.createFeatureSlots("directStrand" + i, "direct",false);
		}
	}
}
